﻿package
{
	import flash.display.*;
	import flash.events.MouseEvent;
	import fl.data.DataProvider;
	import fl.controls.ComboBox;
	import fl.controls.Button;
	import fl.controls.DataGrid;
	import fl.controls.dataGridClasses.DataGridColumn;
	import flash.events.*;
	import fl.events.DataChangeEvent;
	import flash.desktop.NativeApplication;
	
	public class TaskSelect extends MovieClip
	{
		public static var task_list:DataGrid;
		public static var assignee:ComboBox;
		public static var status_box:ComboBox;
		public static var myParent:MovieClip;
		
		private static var taskColors:Array;
		private static var current_params:Object;
		
		public function TaskSelect()
		{
			task_list = TaskList;

			assignee = new ComboBox;
			assignee.x = 11;
			assignee.y = 31;
			assignee.width = 140;
			assignee.rowCount = 15;
			this.addChild(assignee);

			status_box = new ComboBox;
			status_box.x = 160;
			status_box.y = 31;
			status_box.width = 140;
			status_box.rowCount = 20;
			this.addChild(status_box);
			
			assignee.dropdownWidth = 
				status_box.dropdownWidth = 200;
				
			assignee.dropdown.height = 200;

			current_params = new Object();
			taskColors = new Array();
			
			myParent = NativeApplication.nativeApplication.openedWindows[0].stage.getChildAt(0);
			
			var dataProviders:Object = myParent.get_data_providers();
			assignee.dataProvider = dataProviders.people;
			status_box.dataProvider = dataProviders.statuses;
			task_list.dataProvider = dataProviders.tasks;
			
			var priority:DataGridColumn = new DataGridColumn('priority');
			priority.cellRenderer = 'TaskColor';
			priority.width = 10;
			task_list.addColumn(priority);
			
			var tasknumber:DataGridColumn = new DataGridColumn('localid');
			tasknumber.width = 50;
			tasknumber.headerText = '#';
			task_list.addColumn(tasknumber);
			
			var tasktitle:DataGridColumn = new DataGridColumn('title');
			tasktitle.headerText = 'Title';
			task_list.addColumn(tasktitle);
			
			dataProviders.tasks.removeAll();
			dataProviders.tasks.addEventListener(DataChangeEvent.DATA_CHANGE, updateTaskColors, false, 0, true);
			
			status_box.addEventListener(Event.CHANGE, change_status, false, 0, true);
			assignee.addEventListener(Event.CHANGE, change_person, false, 0, true);
			this.dragBar.addEventListener(MouseEvent.MOUSE_DOWN, drag_me, false, 0, true);
			this.cancel_button.addEventListener(MouseEvent.CLICK, hideSettings, false, 0, true);
			this.select_button.addEventListener(MouseEvent.CLICK, selectTask, false, 0, true);
			
			var url_params:Object = new Object();
			url_params.status_id = current_params.status_id = '63859';
			url_params.limit = current_params.limit = '100';

			url_params = merge_params(url_params);

			Intervals.get_tasks(url_params);
			var settings = new Settings();
			assignee.selectedIndex = findItemIndex(assignee, settings.get_setting('user_id'));
			status_box.selectedIndex = findItemIndex(status_box, current_params.status_id.split('=')[1]);
		}

		private function findItemIndex (element:ComboBox, dataString:String):int {
			var index:int = 0;
			for (var i = 0; i < element.length; i++) {
				try
				{
					trace(dataString, element.getItemAt(i).id);

					if (element.getItemAt(i).id == dataString) {
						index = i;
						break;
					}
					else {
					}
				}
				catch(e){}
			}
			trace(index);
			return index;
		}

		function updateTaskColors(e:DataChangeEvent)
		{
			var _data = e.target.toArray();
			if(_data[0])
			{
				task_list.setStyle("CellRenderer", TaskColor);
			}
		}

		function merge_params(url_params)
		{
			for (s in url_params) {
			    current_params[s]=url_params[s];
			}
			return current_params;
		}
		
		function change_status(e:Event):void
		{
			var url_params:Object = new Object();
			var current_status = e.target.selectedItem;
			url_params.status_id = current_status.id;
			
			Intervals.get_tasks(url_params);
		}
		
		function change_person(e:Event):void
		{
			var url_params:Object = new Object();
			var current_person = e.target.selectedItem;
			url_params.assigneeid = current_person.id;
			
			Intervals.get_tasks(url_params);
		}
		
		function hideSettings(e)
		{
			this.parent.removeChild(e.target.parent);
		}
		
		function selectTask(e:MouseEvent):void
		{
			if(task_list.selectedItem)
			{
//				var task:Tasks = new Tasks();
//				var selected_task = Intervals.get_task(task_list.selectedItem.
//				task.set_details(taskXML);
//				task.insert();

				myParent.set_task(task_list.selectedItem);
				this.parent.removeChild(e.target.parent);
			}
		}

		function drag_me(e:MouseEvent)
		{
			stage.nativeWindow.startMove();
		}

		public static function get_myParent()
		{
			return myParent;// = p;
		}
	}
}